1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.primitives;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.base.Converter;
21  import com.google.common.collect.testing.Helpers;
22  
23  import junit.framework.TestCase;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.List;
30  
31  /**
32   * Unit test for {@link Ints}.
33   *
34   * @author Kevin Bourrillion
35   */
36  @GwtCompatible(emulated = true)
37  @SuppressWarnings("cast") // redundant casts are intentional and harmless
38  public class IntsTest extends TestCase {
39    private static final int[] EMPTY = {};
40    private static final int[] ARRAY1 = {(int) 1};
41    private static final int[] ARRAY234
42        = {(int) 2, (int) 3, (int) 4};
43  
44    private static final int LEAST = Integer.MIN_VALUE;
45    private static final int GREATEST = Integer.MAX_VALUE;
46  
47    private static final int[] VALUES =
48        { LEAST, (int) -1, (int) 0, (int) 1, GREATEST };
49  
50    public void testHashCode() {
51      for (int value : VALUES) {
52        assertEquals(((Integer) value).hashCode(), Ints.hashCode(value));
53      }
54    }
55  
56    public void testCheckedCast() {
57      for (int value : VALUES) {
58        assertEquals(value, Ints.checkedCast((long) value));
59      }
60      assertCastFails(GREATEST + 1L);
61      assertCastFails(LEAST - 1L);
62      assertCastFails(Long.MAX_VALUE);
63      assertCastFails(Long.MIN_VALUE);
64    }
65  
66    public void testSaturatedCast() {
67      for (int value : VALUES) {
68        assertEquals(value, Ints.saturatedCast((long) value));
69      }
70      assertEquals(GREATEST, Ints.saturatedCast(GREATEST + 1L));
71      assertEquals(LEAST, Ints.saturatedCast(LEAST - 1L));
72      assertEquals(GREATEST, Ints.saturatedCast(Long.MAX_VALUE));
73      assertEquals(LEAST, Ints.saturatedCast(Long.MIN_VALUE));
74    }
75  
76    private static void assertCastFails(long value) {
77      try {
78        Ints.checkedCast(value);
79        fail("Cast to int should have failed: " + value);
80      } catch (IllegalArgumentException ex) {
81        assertTrue(value + " not found in exception text: " + ex.getMessage(),
82            ex.getMessage().contains(String.valueOf(value)));
83      }
84    }
85  
86    public void testCompare() {
87      for (int x : VALUES) {
88        for (int y : VALUES) {
89          // note: spec requires only that the sign is the same
90          assertEquals(x + ", " + y,
91                       Integer.valueOf(x).compareTo(y),
92                       Ints.compare(x, y));
93        }
94      }
95    }
96  
97    public void testContains() {
98      assertFalse(Ints.contains(EMPTY, (int) 1));
99      assertFalse(Ints.contains(ARRAY1, (int) 2));
100     assertFalse(Ints.contains(ARRAY234, (int) 1));
101     assertTrue(Ints.contains(new int[] {(int) -1}, (int) -1));
102     assertTrue(Ints.contains(ARRAY234, (int) 2));
103     assertTrue(Ints.contains(ARRAY234, (int) 3));
104     assertTrue(Ints.contains(ARRAY234, (int) 4));
105   }
106 
107   public void testIndexOf() {
108     assertEquals(-1, Ints.indexOf(EMPTY, (int) 1));
109     assertEquals(-1, Ints.indexOf(ARRAY1, (int) 2));
110     assertEquals(-1, Ints.indexOf(ARRAY234, (int) 1));
111     assertEquals(0, Ints.indexOf(
112         new int[] {(int) -1}, (int) -1));
113     assertEquals(0, Ints.indexOf(ARRAY234, (int) 2));
114     assertEquals(1, Ints.indexOf(ARRAY234, (int) 3));
115     assertEquals(2, Ints.indexOf(ARRAY234, (int) 4));
116     assertEquals(1, Ints.indexOf(
117         new int[] { (int) 2, (int) 3, (int) 2, (int) 3 },
118         (int) 3));
119   }
120 
121   public void testIndexOf_arrayTarget() {
122     assertEquals(0, Ints.indexOf(EMPTY, EMPTY));
123     assertEquals(0, Ints.indexOf(ARRAY234, EMPTY));
124     assertEquals(-1, Ints.indexOf(EMPTY, ARRAY234));
125     assertEquals(-1, Ints.indexOf(ARRAY234, ARRAY1));
126     assertEquals(-1, Ints.indexOf(ARRAY1, ARRAY234));
127     assertEquals(0, Ints.indexOf(ARRAY1, ARRAY1));
128     assertEquals(0, Ints.indexOf(ARRAY234, ARRAY234));
129     assertEquals(0, Ints.indexOf(
130         ARRAY234, new int[] { (int) 2, (int) 3 }));
131     assertEquals(1, Ints.indexOf(
132         ARRAY234, new int[] { (int) 3, (int) 4 }));
133     assertEquals(1, Ints.indexOf(ARRAY234, new int[] { (int) 3 }));
134     assertEquals(2, Ints.indexOf(ARRAY234, new int[] { (int) 4 }));
135     assertEquals(1, Ints.indexOf(new int[] { (int) 2, (int) 3,
136         (int) 3, (int) 3, (int) 3 },
137         new int[] { (int) 3 }
138     ));
139     assertEquals(2, Ints.indexOf(
140         new int[] { (int) 2, (int) 3, (int) 2,
141             (int) 3, (int) 4, (int) 2, (int) 3},
142         new int[] { (int) 2, (int) 3, (int) 4}
143     ));
144     assertEquals(1, Ints.indexOf(
145         new int[] { (int) 2, (int) 2, (int) 3,
146             (int) 4, (int) 2, (int) 3, (int) 4},
147         new int[] { (int) 2, (int) 3, (int) 4}
148     ));
149     assertEquals(-1, Ints.indexOf(
150         new int[] { (int) 4, (int) 3, (int) 2},
151         new int[] { (int) 2, (int) 3, (int) 4}
152     ));
153   }
154 
155   public void testLastIndexOf() {
156     assertEquals(-1, Ints.lastIndexOf(EMPTY, (int) 1));
157     assertEquals(-1, Ints.lastIndexOf(ARRAY1, (int) 2));
158     assertEquals(-1, Ints.lastIndexOf(ARRAY234, (int) 1));
159     assertEquals(0, Ints.lastIndexOf(
160         new int[] {(int) -1}, (int) -1));
161     assertEquals(0, Ints.lastIndexOf(ARRAY234, (int) 2));
162     assertEquals(1, Ints.lastIndexOf(ARRAY234, (int) 3));
163     assertEquals(2, Ints.lastIndexOf(ARRAY234, (int) 4));
164     assertEquals(3, Ints.lastIndexOf(
165         new int[] { (int) 2, (int) 3, (int) 2, (int) 3 },
166         (int) 3));
167   }
168 
169   public void testMax_noArgs() {
170     try {
171       Ints.max();
172       fail();
173     } catch (IllegalArgumentException expected) {
174     }
175   }
176 
177   public void testMax() {
178     assertEquals(LEAST, Ints.max(LEAST));
179     assertEquals(GREATEST, Ints.max(GREATEST));
180     assertEquals((int) 9, Ints.max(
181         (int) 8, (int) 6, (int) 7,
182         (int) 5, (int) 3, (int) 0, (int) 9));
183   }
184 
185   public void testMin_noArgs() {
186     try {
187       Ints.min();
188       fail();
189     } catch (IllegalArgumentException expected) {
190     }
191   }
192 
193   public void testMin() {
194     assertEquals(LEAST, Ints.min(LEAST));
195     assertEquals(GREATEST, Ints.min(GREATEST));
196     assertEquals((int) 0, Ints.min(
197         (int) 8, (int) 6, (int) 7,
198         (int) 5, (int) 3, (int) 0, (int) 9));
199   }
200 
201   public void testConcat() {
202     assertTrue(Arrays.equals(EMPTY, Ints.concat()));
203     assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY)));
204     assertTrue(Arrays.equals(EMPTY, Ints.concat(EMPTY, EMPTY, EMPTY)));
205     assertTrue(Arrays.equals(ARRAY1, Ints.concat(ARRAY1)));
206     assertNotSame(ARRAY1, Ints.concat(ARRAY1));
207     assertTrue(Arrays.equals(ARRAY1, Ints.concat(EMPTY, ARRAY1, EMPTY)));
208     assertTrue(Arrays.equals(
209         new int[] {(int) 1, (int) 1, (int) 1},
210         Ints.concat(ARRAY1, ARRAY1, ARRAY1)));
211     assertTrue(Arrays.equals(
212         new int[] {(int) 1, (int) 2, (int) 3, (int) 4},
213         Ints.concat(ARRAY1, ARRAY234)));
214   }
215 
216   public void testEnsureCapacity() {
217     assertSame(EMPTY, Ints.ensureCapacity(EMPTY, 0, 1));
218     assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 0, 1));
219     assertSame(ARRAY1, Ints.ensureCapacity(ARRAY1, 1, 1));
220     assertTrue(Arrays.equals(
221         new int[] {(int) 1, (int) 0, (int) 0},
222         Ints.ensureCapacity(ARRAY1, 2, 1)));
223   }
224 
225   public void testEnsureCapacity_fail() {
226     try {
227       Ints.ensureCapacity(ARRAY1, -1, 1);
228       fail();
229     } catch (IllegalArgumentException expected) {
230     }
231     try {
232       // notice that this should even fail when no growth was needed
233       Ints.ensureCapacity(ARRAY1, 1, -1);
234       fail();
235     } catch (IllegalArgumentException expected) {
236     }
237   }
238 
239   public void testJoin() {
240     assertEquals("", Ints.join(",", EMPTY));
241     assertEquals("1", Ints.join(",", ARRAY1));
242     assertEquals("1,2", Ints.join(",", (int) 1, (int) 2));
243     assertEquals("123",
244         Ints.join("", (int) 1, (int) 2, (int) 3));
245   }
246 
247   public void testLexicographicalComparator() {
248     List<int[]> ordered = Arrays.asList(
249         new int[] {},
250         new int[] {LEAST},
251         new int[] {LEAST, LEAST},
252         new int[] {LEAST, (int) 1},
253         new int[] {(int) 1},
254         new int[] {(int) 1, LEAST},
255         new int[] {GREATEST, GREATEST - (int) 1},
256         new int[] {GREATEST, GREATEST},
257         new int[] {GREATEST, GREATEST, GREATEST});
258 
259     Comparator<int[]> comparator = Ints.lexicographicalComparator();
260     Helpers.testComparator(comparator, ordered);
261   }
262 
263   public void testToArray() {
264     // need explicit type parameter to avoid javac warning!?
265     List<Integer> none = Arrays.<Integer>asList();
266     assertTrue(Arrays.equals(EMPTY, Ints.toArray(none)));
267 
268     List<Integer> one = Arrays.asList((int) 1);
269     assertTrue(Arrays.equals(ARRAY1, Ints.toArray(one)));
270 
271     int[] array = {(int) 0, (int) 1, (int) 0xdeadbeef};
272 
273     List<Integer> three = Arrays.asList((int) 0, (int) 1, (int) 0xdeadbeef);
274     assertTrue(Arrays.equals(array, Ints.toArray(three)));
275 
276     assertTrue(Arrays.equals(array, Ints.toArray(Ints.asList(array))));
277   }
278 
279   public void testToArray_threadSafe() {
280     for (int delta : new int[] { +1, 0, -1 }) {
281       for (int i = 0; i < VALUES.length; i++) {
282         List<Integer> list = Ints.asList(VALUES).subList(0, i);
283         Collection<Integer> misleadingSize =
284             Helpers.misleadingSizeCollection(delta);
285         misleadingSize.addAll(list);
286         int[] arr = Ints.toArray(misleadingSize);
287         assertEquals(i, arr.length);
288         for (int j = 0; j < i; j++) {
289           assertEquals(VALUES[j], arr[j]);
290         }
291       }
292     }
293   }
294 
295   public void testToArray_withNull() {
296     List<Integer> list = Arrays.asList((int) 0, (int) 1, null);
297     try {
298       Ints.toArray(list);
299       fail();
300     } catch (NullPointerException expected) {
301     }
302   }
303 
304   public void testToArray_withConversion() {
305     int[] array = {0, 1, 2};
306 
307     List<Byte> bytes = Arrays.asList((byte) 0, (byte) 1, (byte) 2);
308     List<Short> shorts = Arrays.asList((short) 0, (short) 1, (short) 2);
309     List<Integer> ints = Arrays.asList(0, 1, 2);
310     List<Float> floats = Arrays.asList((float) 0, (float) 1, (float) 2);
311     List<Long> longs = Arrays.asList((long) 0, (long) 1, (long) 2);
312     List<Double> doubles = Arrays.asList((double) 0, (double) 1, (double) 2);
313 
314     assertTrue(Arrays.equals(array, Ints.toArray(bytes)));
315     assertTrue(Arrays.equals(array, Ints.toArray(shorts)));
316     assertTrue(Arrays.equals(array, Ints.toArray(ints)));
317     assertTrue(Arrays.equals(array, Ints.toArray(floats)));
318     assertTrue(Arrays.equals(array, Ints.toArray(longs)));
319     assertTrue(Arrays.equals(array, Ints.toArray(doubles)));
320   }
321 
322   public void testAsList_isAView() {
323     int[] array = {(int) 0, (int) 1};
324     List<Integer> list = Ints.asList(array);
325     list.set(0, (int) 2);
326     assertTrue(Arrays.equals(new int[] {(int) 2, (int) 1}, array));
327     array[1] = (int) 3;
328     assertEquals(Arrays.asList((int) 2, (int) 3), list);
329   }
330 
331   public void testAsList_toArray_roundTrip() {
332     int[] array = { (int) 0, (int) 1, (int) 2 };
333     List<Integer> list = Ints.asList(array);
334     int[] newArray = Ints.toArray(list);
335 
336     // Make sure it returned a copy
337     list.set(0, (int) 4);
338     assertTrue(Arrays.equals(
339         new int[] { (int) 0, (int) 1, (int) 2 }, newArray));
340     newArray[1] = (int) 5;
341     assertEquals((int) 1, (int) list.get(1));
342   }
343 
344   // This test stems from a real bug found by andrewk
345   public void testAsList_subList_toArray_roundTrip() {
346     int[] array = { (int) 0, (int) 1, (int) 2, (int) 3 };
347     List<Integer> list = Ints.asList(array);
348     assertTrue(Arrays.equals(new int[] { (int) 1, (int) 2 },
349         Ints.toArray(list.subList(1, 3))));
350     assertTrue(Arrays.equals(new int[] {},
351         Ints.toArray(list.subList(2, 2))));
352   }
353 
354   public void testAsListEmpty() {
355     assertSame(Collections.emptyList(), Ints.asList(EMPTY));
356   }
357 
358   public void testStringConverter_convert() {
359     Converter<String, Integer> converter = Ints.stringConverter();
360     assertEquals((Integer) 1, converter.convert("1"));
361     assertEquals((Integer) 0, converter.convert("0"));
362     assertEquals((Integer) (-1), converter.convert("-1"));
363     assertEquals((Integer) 255, converter.convert("0xff"));
364     assertEquals((Integer) 255, converter.convert("0xFF"));
365     assertEquals((Integer) (-255), converter.convert("-0xFF"));
366     assertEquals((Integer) 255, converter.convert("#0000FF"));
367     assertEquals((Integer) 438, converter.convert("0666"));
368   }
369 
370   public void testStringConverter_convertError() {
371     try {
372       Ints.stringConverter().convert("notanumber");
373       fail();
374     } catch (NumberFormatException expected) {
375     }
376   }
377 
378   public void testStringConverter_nullConversions() {
379     assertNull(Ints.stringConverter().convert(null));
380     assertNull(Ints.stringConverter().reverse().convert(null));
381   }
382 
383   public void testStringConverter_reverse() {
384     Converter<String, Integer> converter = Ints.stringConverter();
385     assertEquals("1", converter.reverse().convert(1));
386     assertEquals("0", converter.reverse().convert(0));
387     assertEquals("-1", converter.reverse().convert(-1));
388     assertEquals("255", converter.reverse().convert(0xff));
389     assertEquals("255", converter.reverse().convert(0xFF));
390     assertEquals("-255", converter.reverse().convert(-0xFF));
391     assertEquals("438", converter.reverse().convert(0666));
392   }
393 
394   public void testTryParse() {
395     tryParseAndAssertEquals(0, "0");
396     tryParseAndAssertEquals(0, "-0");
397     tryParseAndAssertEquals(1, "1");
398     tryParseAndAssertEquals(-1, "-1");
399     tryParseAndAssertEquals(8900, "8900");
400     tryParseAndAssertEquals(-8900, "-8900");
401     tryParseAndAssertEquals(GREATEST, Integer.toString(GREATEST));
402     tryParseAndAssertEquals(LEAST, Integer.toString(LEAST));
403     assertNull(Ints.tryParse(""));
404     assertNull(Ints.tryParse("-"));
405     assertNull(Ints.tryParse("+1"));
406     assertNull(Ints.tryParse("9999999999999999"));
407     assertNull("Max integer + 1",
408         Ints.tryParse(Long.toString(((long) GREATEST) + 1)));
409     assertNull("Max integer * 10",
410         Ints.tryParse(Long.toString(((long) GREATEST) * 10)));
411     assertNull("Min integer - 1",
412         Ints.tryParse(Long.toString(((long) LEAST) - 1)));
413     assertNull("Min integer * 10",
414         Ints.tryParse(Long.toString(((long) LEAST) * 10)));
415     assertNull("Max long", Ints.tryParse(Long.toString(Long.MAX_VALUE)));
416     assertNull("Min long", Ints.tryParse(Long.toString(Long.MIN_VALUE)));
417     assertNull(Ints.tryParse("\u0662\u06f3"));
418   }
419 
420   /**
421    * Applies {@link Ints#tryParse(String)} to the given string and asserts that
422    * the result is as expected.
423    */
424   private static void tryParseAndAssertEquals(Integer expected, String value) {
425     assertEquals(expected, Ints.tryParse(value));
426   }
427 }